package com.mz.ozy.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.mz.ozy.bean.EventBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.utils.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.Nullable;

/**
 * Created by Administrator on 2018/4/26.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.KEY.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == -2) {
                ToastUtils.showToast( "取消支付");
                finish();
            } else if (resp.errCode == 0) {
                ToastUtils.showToast( "支付成功");
                EventBus.getDefault().post(new EventBean(Constants.EventCode.WX_PAY_SUCCESS,null));
                finish();
            } else if (resp.errCode == -1) {
                ToastUtils.showToast( "支付失败");
                finish();
            }
        }
    }
}
